package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.swgk.core.dialog.DialogHelper;

/**
 * @author: 杨畔
 * @date: 3/8/21 2:34 PM
 * @Description: 司机位置页面
 */
public class TransportDriverLocationViewModel extends AndroidViewModel {
    public MutableLiveData<RegeocodeAddress> ldLocation = new MutableLiveData<>();

    public TransportDriverLocationViewModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 根据经纬度查询城市区域
     */
    public void getCityByLocation(Context context, double lat, double lon) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        //通过经纬度获取位置信息
        GeocodeSearch geocodeSearch = new GeocodeSearch(context);
        // 第一个参数表示一个LatLng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
        RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(lat, lon), 200, GeocodeSearch.AMAP);
        geocodeSearch.getFromLocationAsyn(query);
        //搜索位置信息
        geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult result, int code) {
                DialogHelper.dismissProgressDialog();
                if (code != 1000) return;
                RegeocodeAddress address = result.getRegeocodeAddress();
                if (address != null) {
                    ldLocation.postValue(address);
                }
            }

            @Override
            public void onGeocodeSearched(GeocodeResult result, int code) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
